package com.qms.qep.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.qms.common.annotation.Log;
import com.qms.common.core.controller.BaseController;
import com.qms.common.core.domain.AjaxResult;
import com.qms.common.enums.BusinessType;
import com.qms.qep.domain.Batch;
import com.qms.qep.service.IBatchService;
import com.qms.common.utils.poi.ExcelUtil;
import com.qms.common.core.page.TableDataInfo;
import com.qms.qep.domain.vo.BatchTraceVO;

/**
 * 批次Controller
 * 
 * @author qms
 */
@RestController
@RequestMapping("/qep/batch")
public class BatchController extends BaseController
{
    @Autowired
    private IBatchService batchService;

    /**
     * 查询批次列表
     */
    @PreAuthorize("@ss.hasPermi('qep:batch:list')")
    @GetMapping("/list")
    public TableDataInfo list(Batch batch)
    {
        startPage();
        List<Batch> list = batchService.selectBatchList(batch);
        return getDataTable(list);
    }
    
    /**
     * 根据产品ID获取批次列表
     */
    @GetMapping("/listByProduct")
    public AjaxResult listByProductId(@RequestParam(required = false) Integer productId)
    {
        if (productId != null)
        {
            Long productIdLong = productId.longValue();
            List<Batch> list = batchService.getBatchesByProductId(productIdLong);
            return AjaxResult.success(list);
        }
        // 如果未提供产品ID，返回所有批次
        List<Batch> list = batchService.selectBatchList(new Batch());
        return AjaxResult.success(list);
    }

    /**
     * 导出批次列表
     */
    @PreAuthorize("@ss.hasPermi('qep:batch:export')")
    @Log(title = "批次", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Batch batch)
    {
        List<Batch> list = batchService.selectBatchList(batch);
        ExcelUtil<Batch> util = new ExcelUtil<Batch>(Batch.class);
        return util.exportExcel(list, "批次数据");
    }

    /**
     * 获取批次详细信息
     */
    @PreAuthorize("@ss.hasPermi('qep:batch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        Long idLong = id.longValue();
        return AjaxResult.success(batchService.selectBatchById(idLong));
    }

    /**
     * 新增批次
     */
    @PreAuthorize("@ss.hasPermi('qep:batch:add')")
    @Log(title = "批次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Batch batch)
    {
        return toAjax(batchService.insertBatch(batch));
    }

    /**
     * 修改批次
     */
    @PreAuthorize("@ss.hasPermi('qep:batch:edit')")
    @Log(title = "批次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Batch batch)
    {
        return toAjax(batchService.updateBatch(batch));
    }

    /**
     * 删除批次
     */
    @PreAuthorize("@ss.hasPermi('qep:batch:remove')")
    @Log(title = "批次", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        Long[] idsLong = new Long[ids.length];
        for (int i = 0; i < ids.length; i++) {
            idsLong[i] = ids[i].longValue();
        }
        return toAjax(batchService.deleteBatchByIds(idsLong));
    }
    
    /**
     * 批次追溯
     */
    @GetMapping("/trace/{batchNo}")
    public AjaxResult traceBatch(@PathVariable("batchNo") String batchNo)
    {
        BatchTraceVO traceInfo = batchService.traceBatch(batchNo);
        return success(traceInfo);
    }
    
    /**
     * 根据产品ID获取批次列表
     */
    @GetMapping("/product/{productId}")
    public AjaxResult getBatchesByProduct(@PathVariable("productId") Long productId)
    {
        Batch batch = new Batch();
        batch.setProductId(productId);
        List<Batch> list = batchService.selectBatchList(batch);
        return success(list);
    }
    
    /**
     * 根据产品名称获取批次列表
     */
    @GetMapping("/productName/{productName}")
    public AjaxResult getBatchesByProductName(@PathVariable("productName") String productName)
    {
        try {
            List<Batch> list = batchService.getBatchesByProductName(productName);
            System.out.println("根据产品名称 " + productName + " 查询到的批次数量: " + (list != null ? list.size() : 0));
            return AjaxResult.success("查询成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取批次列表失败: " + e.getMessage());
        }
    }
} 